package com.lagou.buffer;

import java.nio.ByteBuffer;

/**
 * @description:
 * @author: yx
 * @date: 2021/11/5 10:13
 */
public class AddBufferDemo {

    public static void main(String[] args) {
        //1. 创建一个没有数据的缓存区
        ByteBuffer allocate = ByteBuffer.allocate(10);
        System.out.println("position,获取到缓冲区当前索引的位置: " + allocate.position());
        System.out.println("limit,获取缓冲区最大索引操作的位置: " + allocate.limit());
        System.out.println("remaining,缓存区剩下能操作的数量: " + allocate.remaining());
        System.out.println("capacity,缓存区总容量: " + allocate.capacity());
        System.out.println("======================================");
        //allocate.put((byte) 1);
        System.out.println("position: " + allocate.position());
        System.out.println("limit: " + allocate.limit());
        System.out.println("remaining: " + allocate.remaining());
        System.out.println("capacity: " + allocate.capacity());
        System.out.println("======================================");
        allocate.put("abcdd".getBytes());
        System.out.println("position: " + allocate.position());
        System.out.println("limit: " + allocate.limit());
        System.out.println("remaining: " + allocate.remaining());
        System.out.println("capacity: " + allocate.capacity());
        System.out.println("读模式======================================");
        allocate.flip();
        System.out.println("position: " + allocate.position());
        System.out.println("limit: " + allocate.limit());
        System.out.println("remaining: " + allocate.remaining());
        System.out.println("capacity: " + allocate.capacity());
        for (int i = 0; i < 5; i++) {
            System.out.println(allocate.get());
        }
        System.out.println("写模式======================================");
        allocate.clear();
        System.out.println("position: " + allocate.position());
        System.out.println("limit: " + allocate.limit());
        System.out.println("remaining: " + allocate.remaining());
        System.out.println("capacity: " + allocate.capacity());
        System.out.println("设置position为5================");
        allocate.position(5);
        System.out.println("position: " + allocate.position());
        System.out.println("limit: " + allocate.limit());
        System.out.println("remaining: " + allocate.remaining());
        System.out.println("capacity: " + allocate.capacity());
    }
}
